package utils

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"time"
)

func GetUserId(c *gin.Context) int64 {
	userId, _ := c.Get("role_id")
	return userId.(int64)
}

func RoleIdToSocketUserId(uid int64) string {
	return fmt.Sprintf("%d", uid)
}

func GetSocketUserId(c *gin.Context) string {
	userId, _ := c.Get("role_id")
	return UserIdToSocketId(userId.(int64))
}

func UserIdToSocketId(userId int64) string {
	return fmt.Sprintf("%d", userId)
}

func CreateConnId(userId string) string {
	connId := fmt.Sprintf("user_id_%s_time_%s_rand_number_%d", userId, time.Now(), rand.Intn(999999))
	return Md5(connId)
}

func Md5(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}

func If(condition bool, trueVal, falseVal interface{}) interface{} {
	if condition {
		return trueVal
	}
	return falseVal

}
